package admin

import (
	"SSO/controllers"
	"SSO/models"
	"fmt"
	"github.com/gin-gonic/gin"
)

type Service struct {
	controllers.Controller
}

func (this Service) List(c *gin.Context) {
	services, err := models.Services{}.GetAll()
	if err != nil {
		msg := fmt.Sprintf("查询服务信息失败 err[%s]", err.Error())
		this.ReturnJson(-1, msg, nil, c)
		return
	}

	this.ReturnJson(0, "success", gin.H{"services": services}, c)
}

func (this Service) Create(c *gin.Context) {
	username := this.SessionGet("username", c).(string)
	b, err := models.Admins{}.IsAdmin(username)
	if err != nil {
		this. ReturnJson(-1, err.Error(), nil, c)
		return
	}
	if !b {
		this. ReturnJson(-1, "无权操作", nil, c)
		return
	}
	name 		:= this.GetDefaultStr("name", "", c)
	usn			:= this.GetDefaultStr("usn", "", c)
	introduce	:= this.GetDefaultStr("introduce", "", c)
	domain 		:= this.GetDefaultStr("domain", "", c)
	pathLogin 	:= this.GetDefaultStr("path_login", "", c)
	pathLogout 	:= this.GetDefaultStr("path_logout", "", c)

	serviceModel := models.Services{}
	if err := serviceModel.CheckInfo(-1, name, usn, domain, pathLogin, pathLogout); err != nil{
		this.ReturnJson(-1, err.Error(), nil, c)
		return
	}

	_, err = serviceModel.Create(name, usn, domain, introduce, pathLogin, pathLogout)

	if err != nil {
		msg := fmt.Sprintf("数据库添加失败 err[%s]", err.Error())
		this. ReturnJson(-1, msg, nil, c)
		return
	}

	this.ReturnJson(0, "success", nil, c)
}

func (this Service) Delete(c *gin.Context) {
	username := this.SessionGet("username", c).(string)
	b, err := models.Admins{}.IsAdmin(username)
	if err != nil {
		this. ReturnJson(-1, err.Error(), nil, c)
		return
	}
	if !b {
		this. ReturnJson(-1, "无权操作", nil, c)
		return
	}

	id := this.GetDefaultInt("id", 0, c)
	err = models.Services{}.DeleteById(id)
	if err != nil {
		msg := fmt.Sprintf("数据库删除失败 err[%s]", err.Error())
		this. ReturnJson(-1, msg, nil, c)
		return
	}

	this.ReturnJson(0, "success", nil, c)
}
func (this Service) Update(c *gin.Context) {
	username := this.SessionGet("username", c).(string)
	b, err := models.Admins{}.IsAdmin(username)
	if err != nil {
		this. ReturnJson(-1, err.Error(), nil, c)
		return
	}
	if !b {
		this. ReturnJson(-1, "无权操作", nil, c)
		return
	}
	fmt.Println(c.PostForm("name"))
	id 			:= this.GetDefaultInt("id", 0, c)
	name 		:= this.GetDefaultStr("name", "", c)
	usn			:= this.GetDefaultStr("usn", "", c)
	introduce	:= this.GetDefaultStr("introduce", "", c)
	domain 		:= this.GetDefaultStr("domain", "", c)
	pathLogin 	:= this.GetDefaultStr("path_login", "", c)
	pathLogout 	:= this.GetDefaultStr("path_logout", "", c)

	serviceModel := models.Services{}
	if err := serviceModel.CheckInfo(id, name, usn, domain, pathLogin, pathLogout); err != nil{
		this.ReturnJson(-1, err.Error(), nil, c)
		return
	}

	err = serviceModel.UpdateById(id ,name, usn, domain, introduce, pathLogin, pathLogout)
	if err != nil {
		msg := fmt.Sprintf("数据库更新失败 err[%s]", err.Error())
		this. ReturnJson(-1, msg, nil, c)
		return
	}

	this.ReturnJson(0, "success", nil, c)
}

func (this Service) GetById(c *gin.Context) {
	id := this.GetDefaultInt("id", 0, c)
	service, err := models.Services{}.GetById(id)
	if err != nil {
		msg := fmt.Sprintf("查询失败 err[%s]", err.Error())
		this. ReturnJson(-1, msg, nil, c)
		return
	}

	this.ReturnJson(0, "success", gin.H{"service":service}, c)
}


